var current = new Array("現在","1分前","      2分前");
var hours = new Array("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
var days = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var months = new Array("4","5","6","7","8","9","10","11","12","1","2","3");


var targetMonthData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroTargetMonthData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var compareMonthData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroCompareMonthData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");

var targetHourData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var ZeroTargetHourData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var compareHourData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var zeroCompareHourData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");

var targetDayData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var zeroTargetDayData = new Array("0","0","0","4","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var compareDayData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var zeroCompareDayData = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0");
var targetDayDataLenght = 0;
var compareDayDataLength = 0;

var currentYearTotalData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroCurrentYearTotalData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var currentYearMaxData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroCurrentYearMaxData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var pastYearTotalData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroPastYearTotalData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var pastYearMaxData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");
var zeroPastYearMaxData = new Array("0","0","0","0","0","0","0","0","0","0","0","0");





//////////////////////////// Data Stores /////////////////////////////
var storeCurrent = new Ext.data.JsonStore({
    fields: ['name', 'data1'],
   //data: generateHoursData()
});

var storeHours = new Ext.data.JsonStore({
    fields: ['name', 'data1', 'data2'],
   data: generateHoursData()
});

var storeDays = new Ext.data.JsonStore({
    fields: ['name', 'data1', 'data2'],
    data: generateDaysData(31)
});

var storeMonths = new Ext.data.JsonStore({
    fields: ['name', 'data1', 'data2'],
   // data: generateMonthsData(12)
});

var storeStatistic = new Ext.data.JsonStore({
    fields: ['name', 'd1T', 'd1M','d2T','d2M'],
   // data: generateMonthsData(12)
});

var storeTap = new Ext.data.JsonStore({
    fields: ['tapGroupID', 'tapGroupName'],
   // data: generateMonthsData(12)
});





/////////////// Generate moke zero data /////////////////////////
function generateHoursData(n){
    var data = [],i;
    for (i = 0; i < (n || 24); i++) {
        data.push({
            name: hours[i],
            data1: '0',
            data2: '0',
            
        });
    }
    return data;
}

function generateDaysData(n){
    var data = [], i;
    for (i = 0; i < (n || 31); i++) {
        data.push({
            name: days[i],
            data1: '0',
            data2: '0',
            
        });
    }
    return data;
}

function generateMonthsData(n){
    var data = [], i;
    for (i = 0; i < (n || 12); i++) {
        data.push({
            name: months[i],
            data1: '0',
            data2: '0',
            
        });
    }
    return data;
}






//////////////////////// Methods for add Data to Data Stores //////////////////////////

function addDataToStore(dataStore,dataModel,targetData,CompareData){

	var records = dataStore.getRange();
	dataStore.remove(records);
	
	var data = [];
	
    for (var i = 0; i < targetData.length; i++) {
    	data.push({name : dataModel[i], data1 : targetData[i], data2 : CompareData[i]});
    }
    
    dataStore.setData(data);
}

function addDataToDayStore(dataStore,dataModel,targetData,CompareData){
	
	var records = dataStore.getRange();
	dataStore.remove(records);
	
	if(targetDayDataLenght >= compareDayDataLength){
		size = targetDayDataLenght;
	}else{
		size = compareDayDataLength;
	}
	
	var data = [];
	
    for (var i = 0; i < size; i++) {
    	data.push({name : dataModel[i], data1 : targetData[i], data2 : CompareData[i]});
    }
    
    dataStore.setData(data);
}







////////////////////// get total and max //////////////////////////////////

function getTotalUsage(arr){
	var total = 0;
	for(var i=0; i<arr.length; i++){
		total = parseFloat(total)+parseFloat(arr[i]);
	}
	return parseFloat(total).toFixed(3);
}

function getMaxUsage(arr){
	var total = 0;
	for(var i=0; i<arr.length; i++){
		if(arr[i]>total){
			total = arr[i];
		}
	}
	return parseFloat(total).toFixed(3);
}






///////////////////////////////// Current View data handle /////////////////////

function storeCurrentData(v){
	
	var data = [];
	var twomin = (parseFloat(v)*2).toFixed(3);
	
	data.push({name : current[2], data1 : twomin});
	data.push({name : current[1], data1 : v });
	data.push({name : current[0], data1 : v });
	storeCurrent.setData(data);
	
	Ext.getCmp('currentTableView').setItems([{
												xtype: 'panel',
												width: '100%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlcurrentTable(storeCurrent)],
											}]);
}








//////////////////////////////// Hour view data habdle //////////////////////////////////////////////////////

function storeTargetHoursData(v){
	
	targetHourData = ZeroTargetHourData.slice(0);
	
	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			targetHourData[j]=parseFloat(targetHourData[j])+parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]);
			targetHourData[j]= parseFloat(targetHourData[j]).toFixed(0);
		}
		
	}
	addDataToStore(storeHours, hours, targetHourData, compareHourData);
	Ext.getCmp('dayTargetTotal').setValue(parseFloat(getTotalUsage(targetHourData)).toFixed(0)+' Wh');
	Ext.getCmp('dayTargetMax').setValue(parseFloat(getMaxUsage(targetHourData)).toFixed(0)+' W');
	
	Ext.getCmp('HourTableView').setItems([

									        {
									        	xtype: 'panel',
												width: '50%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlHoursTable(storeHours,0,12)],
									        },
									        {
									        	xtype: 'panel',
												width: '50%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlHoursTable(storeHours,12,24)],
									        }
    	
    ]);
	
    
}

function storeCompareHoursData(v){
	
	compareHourData = zeroCompareHourData.slice(0);
	
	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			compareHourData[j]=parseFloat(compareHourData[j])+parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]);
			compareHourData[j]= parseFloat(compareHourData[j]).toFixed(0);
		}
		
	}
	addDataToStore(storeHours, hours, targetHourData, compareHourData);
	Ext.getCmp('dayCompareTotal').setValue(parseFloat(getTotalUsage(compareHourData)).toFixed(0)+' Wh');
	Ext.getCmp('dayCompareMax').setValue(parseFloat(getMaxUsage(compareHourData)).toFixed(0)+' W');
	
	Ext.getCmp('HourTableView').setItems([

									        {
									        	xtype: 'panel',
												width: '50%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlHoursTable(storeHours,0,12)],
									        },
									        {
									        	xtype: 'panel',
												width: '50%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlHoursTable(storeHours,12,24)],
									        }
  	
  ]);
}





/////////////////////////////// Day view data hamdle //////////////////////////////////////////////////////////

function storeTargetDayData(v){
	
	targetDayData = zeroTargetDayData.slice(0);
	targetDayDataLenght = v.response.tap_list[0].data_list[0].amount_electric_power.length;
	
	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			targetDayData[j]=parseFloat(targetDayData[j])+parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]);
			targetDayData[j]= parseFloat(targetDayData[j]).toFixed(0);
		}
		
	}
	addDataToDayStore(storeDays, days, targetDayData, compareDayData, targetDayDataLenght);
	Ext.getCmp('monthTargetTotal').setValue(parseFloat(getTotalUsage(targetDayData)).toFixed(0)+' Wh');
	Ext.getCmp('monthTargetMax').setValue(parseFloat(getMaxUsage(targetDayData)).toFixed(0)+' W');
	
	Ext.getCmp('DayTableView').setItems([
	                                      {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,0,10)],
									        },
									        {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,10,20)],
									        },
									        {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,20,-1)],
									        }
    ]);
	
    
}

function storeCompareDayData(v){
	
	compareDayData = zeroCompareDayData.slice(0);
	compareDayDataLength = v.response.tap_list[0].data_list[0].amount_electric_power.length;
	
	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			compareDayData[j]=parseFloat(compareDayData[j])+parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]);
			compareDayData[j]= parseFloat(compareDayData[j]).toFixed(0);
		}
		
	}
	addDataToDayStore(storeDays, days, targetDayData, compareDayData);
	Ext.getCmp('monthCompareTotal').setValue(parseFloat(getTotalUsage(compareDayData)).toFixed(0)+' Wh');
	Ext.getCmp('monthCompareMax').setValue(parseFloat(getMaxUsage(compareDayData)).toFixed(0)+' W');
	
	Ext.getCmp('DayTableView').setItems([
	                                      {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,0,10)],
									        },
									        {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,10,20)],
									        },
									        {
									        	xtype: 'panel',
												width: '33%',
												height : '100%',
												styleHtmlContent: true,
												html: [ generateHtmlDaysTable(storeDays,20,-1)],
									        }
    ]);
}







////////////////////////////////Month view data handle //////////////////////////////////////////////////////

function storeTargetMonthsData(v){
	
	targetMonthData = zeroTargetMonthData.slice(0);
	
	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			targetMonthData[j]=parseFloat(targetMonthData[j])+(parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000;
			targetMonthData[j] = parseFloat(targetMonthData[j]).toFixed(3);
		}
	
	}
	addDataToStore(storeMonths, months, targetMonthData, compareMonthData);
	Ext.getCmp('yearTargetTotal').setValue(parseFloat(getTotalUsage(targetMonthData)).toFixed(1)+' kWh');
	Ext.getCmp('yearTargetMax').setValue(parseFloat(getMaxUsage(targetMonthData)).toFixed(1)+' kW');
	
	Ext.getCmp('monthTableView').setItems([ {
		xtype: 'panel',
		width: '100%',
		height : '100%',
		styleHtmlContent: true,
		html: [generateHtmlMonthsTable(storeMonths)],
	}]);
}

function storeCompareMonthsData(v){
	compareMonthData = zeroCompareMonthData.slice(0);

	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			compareMonthData[j]=parseFloat(compareMonthData[j])+(parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000;
			compareMonthData[j]= parseFloat(compareMonthData[j]).toFixed(3);
		}
	
	}
	addDataToStore(storeMonths, months, targetMonthData, compareMonthData);
	Ext.getCmp('yearCompareTotal').setValue(parseFloat(getTotalUsage(compareMonthData)).toFixed(1)+' kWh');
	Ext.getCmp('yearCompareMax').setValue(parseFloat(getMaxUsage(compareMonthData)).toFixed(1)+' kW');
	
	Ext.getCmp('monthTableView').setItems([ {
		xtype: 'panel',
		width: '100%',
		height : '100%',
		styleHtmlContent: true,
		html: [generateHtmlMonthsTable(storeMonths)],
	}]);

}








////////////////////////////////statistic view data handle //////////////////////////////////////////////////////

function storeCurrentYearStatisticsData(v){
	
	currentYearTotalData = zeroCurrentYearTotalData.slice(0);
	currentYearMaxData = zeroCurrentYearMaxData.slice(0);

	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			
			if(parseFloat(currentYearMaxData[j]) < parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j])){
				currentYearMaxData[j] = ((parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000).toFixed(3);
			}
			
			currentYearTotalData[j]=parseFloat(currentYearTotalData[j])+(parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000;
			currentYearTotalData[j] = parseFloat(currentYearTotalData[j]).toFixed(3);
		}
	}
}

function storePastYearStatisticsData(v){

	var data = [];
	pastYearTotalData = zeroPastYearTotalData.slice(0);
	pastYearMaxData = zeroPastYearMaxData.slice(0);

	for(var i=0; i<v.response.tap_list.length; i++){
		for(var j=0; j<v.response.tap_list[i].data_list[0].amount_electric_power.length;j++){
			
			if(parseFloat(pastYearMaxData[j]) < parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j])){
				pastYearMaxData[j] = ((parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000).toFixed(3);
			}
			
			pastYearTotalData[j]=parseFloat(pastYearTotalData[j])+(parseFloat(v.response.tap_list[i].data_list[0].amount_electric_power[j]))/1000;
			pastYearTotalData[j] = parseFloat(pastYearTotalData[j]).toFixed(3);
		}
	}
	
    for (var i = 0; i < 12; i++) {
    	data.push({name : months[i], d1T : currentYearTotalData[i], d1M : currentYearMaxData[i], d2T : pastYearTotalData[i], d2M : pastYearMaxData[i]});
    }
    
    storeStatistic.setData(data);
    if(typeof(Storage)!=="undefined")
	  {
		 localStorage.setItem('localStore', JSON.stringify(data));
	  }
	else
	  {
	  alert("Sorry, your browser does not support web storage...");
	  }
	
	Ext.getCmp('statisticTableView').setItems([ {
		xtype: 'panel',
		width: '100%',
		height : '100%',
		styleHtmlContent: true,
		html: [generateHtmlStatisticTable(storeStatistic)],
	}]);
	//alert(storeStatistic.getAt(0).get('d1T'));
}



/////////////////// For Tap Group ///////////////////////////

function StoreTapList(v){
	var data = [];
	data.push({tapGroupID : '0', tapGroupName : 'すべて'});
	for(var i=0; i<v.response.tap_group_list.length; i++){
    	data.push({tapGroupID : v.response.tap_group_list[i].tap_group_id, tapGroupName : v.response.tap_group_list[i].tap_group_name});
    }
    storeTap.setData(data);
}
